<?php

namespace common\logics;

use Yii;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use common\models\dynamic\DynamicStats;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $content
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class DynamicCommentLogic extends \common\models\dynamic\DynamicComment {

    public function fields() {
        return [
            'id',
            'post',
            'time' => 'publishTime',
            'user',
            'device' => function() {
                return $this->device ? $this->device->model : '未知设备';
            },
            'reply_nums' => 'replyCount',
            'binmb',
            'tools',
        ];
    }

    public function getTools() {
        $stats = [
            'likes' => 0,
            'retweets' => 0,
            'comments' => 0,
        ];
        if ($this->stats) {
            $stats['likes'] = $this->stats->likes;
            $stats['retweets'] = $this->stats->retweets;
            $stats['comments'] = $this->stats->comments;
        }
        return $stats;
    }

    public function getStats() {
        return $this->hasOne(DynamicStats::className(), ['comment_id' => 'id'])->andWhere([
            'dynamic_id' => $this->dynamic_id,
        ]);
    }

    public function getPublishTime() {
        if ($this->created_at) {
            return Yii::$app->formatter->asRelativeTime($this->created_at);
        }
        return '很久以前';
    }

    public function getBinmb() {
        return $this->getChildren()->andWhere(['user_id' => $this->user_id]);
    }

    public function getReplyCount() {
        return $this->getChildren()->count();
    }

}
